package com.media.system.service.impl;

import com.media.system.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * 本地文件存储服务实现
 * 
 * @author Claude
 * @since 2024-01-20
 */
@Service
@Slf4j
@ConditionalOnProperty(name = "minio.enabled", havingValue = "false")
public class LocalFileStorageServiceImpl implements FileStorageService {

    @Value("${media.system.upload.path:./uploads}")
    private String uploadPath;

    @Value("${media.system.upload.prefix:http://localhost:8080/files}")
    private String urlPrefix;

    @Override
    public String uploadFile(MultipartFile file, String objectName) {
        try {
            return uploadFile(file.getInputStream(), objectName, file.getContentType(), file.getSize());
        } catch (IOException e) {
            log.error("上传文件失败: {}", e.getMessage(), e);
            throw new RuntimeException("上传文件失败: " + e.getMessage());
        }
    }

    @Override
    public String uploadFile(InputStream inputStream, String objectName, String contentType, long fileSize) {
        try {
            // 构建完整文件路径
            Path targetPath = Paths.get(uploadPath, objectName);
            
            // 确保目录存在
            Files.createDirectories(targetPath.getParent());
            
            // 保存文件
            Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING);
            
            log.info("文件保存成功: {}", targetPath);
            return getFileUrl(objectName);
            
        } catch (IOException e) {
            log.error("保存文件失败: {}", e.getMessage(), e);
            throw new RuntimeException("保存文件失败: " + e.getMessage());
        }
    }

    @Override
    public boolean deleteFile(String objectName) {
        try {
            Path filePath = Paths.get(uploadPath, objectName);
            boolean deleted = Files.deleteIfExists(filePath);
            
            if (deleted) {
                log.info("文件删除成功: {}", filePath);
            }
            return deleted;
            
        } catch (IOException e) {
            log.error("删除文件失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public String getFileUrl(String objectName) {
        return urlPrefix + "/" + objectName.replace("\\", "/");
    }

    @Override
    public String getPresignedUrl(String objectName, int expireTime) {
        // 本地存储不支持预签名URL，返回普通URL
        return getFileUrl(objectName);
    }

    @Override
    public boolean fileExists(String objectName) {
        Path filePath = Paths.get(uploadPath, objectName);
        return Files.exists(filePath);
    }

    @Override
    public long getFileSize(String objectName) {
        try {
            Path filePath = Paths.get(uploadPath, objectName);
            return Files.size(filePath);
        } catch (IOException e) {
            log.error("获取文件大小失败: {}", e.getMessage(), e);
            return 0;
        }
    }
}